package com.gensdai.leliang.remoteInterface;

import com.gensdai.leliang.AppApplication;
import com.gensdai.leliang.retrofitUtils.conver.StringConver;
import com.gensdai.leliang.retrofitUtils.okhttputils.Okhttp3Utils;
import com.github.aurae.retrofit2.LoganSquareConverterFactory;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/** RetrofitFactory 初始化 获取retrofit的对象
 * Created by LT on 2017/6/19.
 */

public class RetrofitFactory {

    private static RetrofitFactory mRetrofitFactory = null;
    private static InterfaceCodeUtils mAPIFunction;


    private RetrofitFactory() {
        //获取okhttp的实例  retrofit使用okhttp来跟后端链接  OKhttp的一些初始化方法请进入方法看
        OkHttpClient okHttpC = Okhttp3Utils.getOkhttpClient(AppApplication.getContext());
        Retrofit mRetrofit = new Retrofit.Builder().baseUrl(URLConfig.BASEURL)
                .client(okHttpC)                                                    //设置retrofit设置用哪种客户端连接
                .addConverterFactory(StringConver.StringConverterFactory.create())  //可以添加多种解析方式 可以直接取字符 如果有多种同样的解析方式，默认使用第一种
                .addConverterFactory(LoganSquareConverterFactory.create())          //返回json处理用logansquare处理
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())          //返回格式统一用Observable
                .build();
        mAPIFunction = mRetrofit.create(InterfaceCodeUtils.class);
    }


    //获取retrofit的实例
//    public static RetrofitFactory getInstance() {
//        if (mRetrofitFactory == null) {
//            synchronized (RetrofitFactory.class) {
//                if (mRetrofitFactory == null) {
//                    mRetrofitFactory = new RetrofitFactory();
//                }
//            }
//        }
//        return mRetrofitFactory;
//    }

    public static RetrofitFactory getInstance(){
        return RetrofitFactoryHolder.sInstance;
    }

    private static class RetrofitFactoryHolder{
        private static final RetrofitFactory sInstance = new RetrofitFactory();
    }

    public  InterfaceCodeUtils API(){
        return mAPIFunction;
    }
}
